package com.xx.pmis.web.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xx.pmis.model.Book;
import com.xx.pmis.model.Medicine;
import com.xx.pmis.model.Present;
import com.xx.pmis.model.Shop;
import com.xx.pmis.model.Wine;
import com.xx.pmis.services.BookService;
import com.xx.pmis.services.MedicineService;
import com.xx.pmis.services.PresentService;
import com.xx.pmis.services.WineService;

/**
 * @class 商店<br>
 * 
 * @editor 石星星 @Date 2011-11-14 下午05:18:02 <br>
 * @version jdk_1.5.0
 */
public class ShopUtil extends PMISBaseUtil {
	private static final Log log = LogFactory.getLog(ShopUtil.class);

	BookService bookService = getPMISServices().getBookService();
	PresentService presentService = getPMISServices().getPresentService();
	WineService wineService = getPMISServices().getWineService();
	MedicineService medicineService = getPMISServices().getMedicineService();

	AccountUtil accountUtil = new AccountUtil();

	public List<Shop> getAllShopIncludeExt(List<Shop> all) {
		Iterator<?> it = all.iterator();
		while (it.hasNext()) {
			// 总金额
			double totalMoney = 0.00;
			// 总次数
			int buyNum = 0;
			Shop shop = (Shop) it.next();
			// 礼品
			List<Present> presents = null;
			try {
				presents = presentService.getAllPresentByCondition(" where shopID = '" + shop.getShopID() + "'");
			} catch (Exception e) {
				log.info("根据商店ID查询该商店下的所有礼物异常！" + e.toString());
			}
			Iterator<?> iPresent = presents.iterator();
			while (iPresent.hasNext()) {
				Present present = (Present) iPresent.next();
				totalMoney = totalMoney + Double.parseDouble(present.getPresentExpenses());
				buyNum = buyNum + 1;
			}
			// 图书
			List<Book> books = null;
			try {
				books = bookService.getAllBookByCondition(" where shopID = '" + shop.getShopID() + "'");
			} catch (Exception e) {
				log.info("根据商店ID查询该商店下的所有图书异常！" + e.toString());
			}
			Iterator<?> iBook = books.iterator();
			while (iBook.hasNext()) {
				Book book = (Book) iBook.next();
				totalMoney = totalMoney + Double.parseDouble(book.getBookExpenses());
				buyNum = buyNum + 1;
			}
			// 葡萄酒
			List<Wine> wines = null;
			try {
				wines = wineService.getAllWineByCondition(" where shopID = '" + shop.getShopID() + "'");
			} catch (Exception e) {
				log.info("根据商店ID查询该商店下的所有图书异常！" + e.toString());
			}
			Iterator<?> iWine = wines.iterator();
			while (iWine.hasNext()) {
				Wine wine = (Wine) iWine.next();
				totalMoney = totalMoney + Double.parseDouble(wine.getWinePrice());
				buyNum = buyNum + 1;
			}
			// 医疗
			List<Medicine> medicines = null;
			try {
				medicines = medicineService.getAllMedicineByCondition(" where shopID = '" + shop.getShopID() + "'");
			} catch (Exception e) {
				log.info("根据商店ID查询该商店下的所有图书异常！" + e.toString());
			}
			Iterator<?> iMedicine = medicines.iterator();
			while (iMedicine.hasNext()) {
				Medicine medicine = (Medicine) iMedicine.next();
				totalMoney = totalMoney + Double.parseDouble(medicine.getMedicineExpense());
				buyNum = buyNum + 1;
			}
			shop.setBuyNum(buyNum);
			shop.setTotalMoney(accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(totalMoney)));
		}
		return all;
	}

	public List<Shop> getAllShopIncludeExtAfterOrdination(List<Shop> all) {
		List<Shop> listShop = new ArrayList<Shop>();

		ComparatorShop comparator = new ComparatorShop();
		Collections.sort(all, comparator);

		for (int i = 0; i < all.size(); i++) {
			Shop shop = (Shop) all.get(i);
			System.out.println(shop.getBuyNum() + "," + shop.getShopName());
			listShop.add(shop);
		}

		return listShop;
	}

}
